package com.yxkj.syh.app.huarong.activities.orders.list;

import android.os.Bundle;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.syh.app.basic.base.BaseFragment;
import com.yxkj.syh.app.huarong.BR;
import com.yxkj.syh.app.huarong.R;
import com.yxkj.syh.app.huarong.adps.OrderListAdp;
import com.yxkj.syh.app.huarong.bean.PagesRequest;
import com.yxkj.syh.app.huarong.constants.ArouterPath;
import com.yxkj.syh.app.huarong.constants.OrderStatus;
import com.yxkj.syh.app.huarong.databinding.FragmentOrderListBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * 运单列表
 */
public class OrderListFragment extends BaseFragment<FragmentOrderListBinding, OrderListVM> {

    int status = OrderStatus.ALL;
    private PagesRequest ordersRequest;
    private OrderListAdp mOrderListAdp;

    public OrderListFragment() {
        // Required empty public constructor
    }

    public static OrderListFragment newInstance(int status) {
        OrderListFragment fragment = new OrderListFragment();
        Bundle args = new Bundle();
        args.putInt("status", status);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getViewModelId() {
        return BR.orderListVM;
    }

    @Override
    protected void initBundleData() {
        status = getArguments().getInt("status", OrderStatus.ALL);
    }

    @Override
    protected int getContentLayout() {
        return R.layout.fragment_order_list;
    }

    @Override
    protected void initView() {
        mOrderListAdp = new OrderListAdp();
        mOrderListAdp.setIvIconRec(R.mipmap.ic_no_order);
        mVDBinding.rvOrders.setLayoutManager(new LinearLayoutManager(getActivity(), RecyclerView.VERTICAL, false));
        mOrderListAdp.bindToRecycleView(mVDBinding.rvOrders);
        mOrderListAdp.setOnViewClick(orderInfo -> {
            if (orderInfo.getStatus() == OrderStatus.WAIT_SHIPMENTS) {
                ARouter.getInstance().build(ArouterPath.ORDER_WAIT_ACTIVITY).withLong("orderId", orderInfo.getId()).navigation();
            } else {
                ARouter.getInstance().build(ArouterPath.ORDER_OTHER_STATUS_ACTIVITY).withLong("orderId", orderInfo.getId()).navigation();
            }
        });

        mVDBinding.srlOrders.setOnRefreshListener(refreshLayout -> {
            mOrderListAdp.clear();
            initData();
        });

        mVDBinding.srlOrders.setOnLoadMoreListener(refreshLayout -> {
            ordersRequest.setPageIndex(ordersRequest.getPageIndex().intValue() + 1);
            mViewModel.getOrderList(ordersRequest);
        });
        mVDBinding.srlOrders.setEnableLoadMore(false);
    }

    @Override
    protected void initData() {
        ordersRequest = new PagesRequest();
        ordersRequest.setPageIndex(1);
        ordersRequest.setPageSize(10);
        ordersRequest.setSortFields("id_d");

        List<PagesRequest.SqlParam> sqlParams = new ArrayList<>();
        if (status == OrderStatus.ALREADY_RECEIVING) {// 已收货  已签收
            PagesRequest.SqlParam sqlParam = new PagesRequest.SqlParam();
            sqlParam.setValue(status);
            sqlParam.setKey("o.status");
            sqlParam.setOper(">=");
            sqlParams.add(sqlParam);
        } else if (status != OrderStatus.ALL) {
            PagesRequest.SqlParam sqlParam = new PagesRequest.SqlParam();
            sqlParam.setValue(status);
            sqlParam.setKey("o.status");
            sqlParam.setOper("=");
            sqlParams.add(sqlParam);
        }
        ordersRequest.setQueryList(sqlParams);

        mViewModel.getOrderList(ordersRequest);
    }

    @Override
    protected void initObservers() {
        super.initObservers();
        mViewModel.mldOrderResponse.observe(this, orderInfos -> {
            if (mVDBinding.srlOrders.isRefreshing()) {
                mVDBinding.srlOrders.finishRefresh();
            } else if (mVDBinding.srlOrders.isLoading()) {
                mVDBinding.srlOrders.finishLoadMore();
            }
            if (orderInfos != null) {
                if (ordersRequest.getPageIndex() == 1) {
                    mOrderListAdp.setNewData(orderInfos.getRecords());
                } else {
                    mOrderListAdp.addAll(orderInfos.getRecords());
                }
                if (ordersRequest.getPageIndex() < orderInfos.getPages()) {
                    mVDBinding.srlOrders.setEnableLoadMore(true);
                } else {
                    mVDBinding.srlOrders.setEnableLoadMore(false);
                }
            }
        });
    }

    @Override
    protected void visibleAgain() {
//        mOrderListAdp.clear();
        initData();
    }
}
